package com.zyz.I_多线程.常用方法;

/**
 * @author: vick
 * @Date: 2025/3/22
 * @Description:
 * @version: 1.0
 */
public class DaemonThread2{
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int i = 0; i < 10; i++) {
                if(i == 9) {
                    System.out.println(Thread.currentThread().getName() + "死了" + i);
                    break;
                }
                System.out.println(Thread.currentThread().getName() + "活着" + i);
            }
        },"主公");

        Thread daemonThread = new Thread(() -> {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int i = 0; i < 100000; i++) {
                System.out.println(Thread.currentThread().getName() + "守护" + i);
            }
        },"士兵");

        //将线程设置为守护线程
        daemonThread.setDaemon(true);

        thread.start();
        daemonThread.start();
    }
}
